package com.gyh.stlserver.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gyh.stlserver.entity.Coach;
import com.gyh.stlserver.entity.Course;
import com.gyh.stlserver.mapper.CoachMapper;
import com.gyh.stlserver.mapper.CourseMapper;
import com.gyh.stlserver.service.CoachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CoachServiceImpl implements CoachService {
    @Autowired
    private CoachMapper coachMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public List<Coach> search(Coach coach) {
        return coachMapper.select(coach);
    }

    @Override
    public PageInfo<Coach> searchByPage(Integer pageNum, Integer pageSize, Coach coach) {
        PageHelper.startPage(pageNum,pageSize);
        List<Coach> coaches = coachMapper.select(coach);
        return new PageInfo<>(coaches);
    }

    @Override
    public int addCoach(Coach coach) {
        return coachMapper.insert(coach);
    }

    @Override
    public Coach getCoachByPhone(String phone) {
        return coachMapper.selectCoachByPhone(phone);
    }

    @Override
    public int removeCoach(Integer id) {
        /*删除之前判断教练是否存在*/
        List<Course> courses = courseMapper.selectCourseByCid(id);
        if (ObjectUtil.isNotEmpty(courses)){
            return -2;
        }
        return coachMapper.deleteCoachById(id);
    }

    @Override
    public int editCoach(Coach coach) {
        return coachMapper.update(coach);
    }

    @Override
    public int delSelected(Integer[] ids) {
        return coachMapper.deleteByIds(ids);
    }
}
